package com.totoro.tyrank.utils;

import com.totoro.tyrank.constants.PatternConst;
import com.totoro.tyrank.entity.TopRangeResult;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

public class PatternUtil {

    public static List<TopRangeResult> extractAllRangeInfo(String inputStr) {
        // 正则表达式匹配<topX-Y>
        Matcher matcher = PatternConst.TOP_RANGE_TAG_PATTERN.matcher(inputStr);

        List<TopRangeResult> results = new ArrayList<>();

        // 循环查找所有匹配项
        while (matcher.find()) {
            // 获取匹配到的完整标记
            String fullTag = matcher.group();
            // 提取开始和结束数字
            int start, end;
            if (matcher.group(1) != null && matcher.group(2) != null) {
                start = Integer.parseInt(matcher.group(1));
                end = Integer.parseInt(matcher.group(2));
            } else {
                start = Integer.parseInt(matcher.group(3));
                end = Integer.parseInt(matcher.group(4));
            }
            // 生成数字集合
            List<Integer> numberList = new ArrayList<>();
            for (int i = start; i <= end; i++) {
                numberList.add(i);
            }
            results.add(new TopRangeResult(numberList, fullTag));
        }
        return results;
    }

}
